<?php

namespace app\model;

use think\Model;

class WechatCatalog extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'wechat_catalog';
    // 设置当前模型主键
    protected $pk = 'id';
    protected $append =[
        "catalog_scale_type_text",
        'catalog_type_text',
        'industry_type_text',
        'catalog_status_text'
    ];
    protected $scale_type_arr=[
        0=>'普通商品库',
        1=>'超大商品库'
    ];
    protected $catalog_type_arr=[
        0=>'标准商品库类型',
    ];
    protected $industry_type_arr=[
        0=>'网服短剧',
    ];
    protected $catalog_status_arr=[
        0=>'初始化状态',
        1=>'有效状态',
        2=>'暂停状态',
        3=>'已删除状态',
    ];
    //商品库规模
    public function getCatalogScaleTypeTextAttr($value,$data){
        if(!isset($data['catalog_scale_type'])||empty($this->scale_type_arr[$data['catalog_scale_type']]))return '';
        return $this->scale_type_arr[$data['catalog_scale_type']];
    }
    //商品库类型
    public function getCatalogTypeTextAttr($value,$data){
        if(!isset($data['catalog_type'])||empty($this->catalog_type_arr[$data['catalog_type']]))return '';
        return $this->catalog_type_arr[$data['catalog_type']];
    }
    //商品库行业
    public function getIndustryTypeTextAttr($value,$data){
        if(!isset($data['industry_type'])||empty($this->industry_type_arr[$data['industry_type']]))return '';
        return $this->industry_type_arr[$data['industry_type']];
    }
    //商品状态
    public function getCatalogStatusTextAttr($value,$data){
        if(!isset($data['catalog_status'])||empty($this->catalog_status_arr[$data['catalog_status']]))return '';
        return $this->catalog_status_arr[$data['catalog_status']];
    }
    /**
     * 新增/更新
     *
     * @dateTime 2022/09/20
     * @author zhixiang0215@126.com
     * @param array $params
     * @return int
     */
    public function edit($params)
    {
        if (empty($params['id'])) {
            $data = $this::create($params);
            return $data->id;
        }
        $this::update($params);
        return $params['id'];
    }

    // 获取分页数据
    public function getPagingDataByWhere($where = [], $page = 1, $limit = 10, $fields = 'id, name', $order = 'id desc')
    {
        $total = $this::where($where)->count();

        if ($total == 0) {
            return ['total' => 0, 'total_page' => 0, 'data' => []];
        }

        $offset = ($page - 1) * $limit;
        $totalPage = ceil($total / $limit);

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }

    /**
     * 根据id获取数据
     * 
     * @dateTime 2022/09/01
     * @author zhixiang0215@126.com
     * @param int id
     * @param string fields
     * @return array
    */
    public function getInfoById(int $id, $fields = 'id')
    {
        $data = $this::where(['id' => $id])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }
    /**
     * 根据条件获取数据
     * 
     * @dateTime 2022/09/05
     * @author zhixiang0215@126.com
     * @param array where
     * @param string fields
     * @return array
     */
    public function getInfoByWhere(array $where, string $fields = 'id')
    {
        $data = $this::where($where)->field($fields)->find();
        return $data ? $data->toArray() : [];
    }
    // 获取所有数据
    public function getDataByWhere($where = [], $fields = 'id, name', $order = 'id asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

}